package com.jjwxc.jwjskandriod.activity;

import static com.bumptech.glide.request.RequestOptions.bitmapTransform;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.google.android.material.tabs.TabLayout;
import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.adapter.DetailsViewHolder;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWork;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.imageloader.FFImageLoader;
import com.jjwxc.jwjskandriod.framework.refresh.XRefreshView;
import com.jjwxc.jwjskandriod.framework.refresh.recyclerview.BaseRecyclerAdapter;
import com.jjwxc.jwjskandriod.framework.utils.FFUtils;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.model.BookAlsoListResponse;
import com.jjwxc.jwjskandriod.model.BookListSimilarResponse;
import com.jjwxc.jwjskandriod.model.DetailsBookResponse;
import com.jjwxc.jwjskandriod.model.VipDistanceResponse;
import com.jjwxc.jwjskandriod.readActivity.ReadActivity;
import com.jjwxc.jwjskandriod.readActivity.bean.CollBookBean;
import com.jjwxc.jwjskandriod.readActivity.bean.JListChapterBean;
import com.jjwxc.jwjskandriod.readActivity.view.TxtChapter;
import com.jjwxc.jwjskandriod.widget.BollotBombPop;
import com.jjwxc.jwjskandriod.widget.DetailsCategoryPop;
import com.jjwxc.jwjskandriod.widget.DetailsDownMorePop;
import com.jjwxc.jwjskandriod.widget.GroupPop;
import com.jjwxc.jwjskandriod.widget.PopViewUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.CropTransformation;

public class BookDetailsActivity extends BaseActivity implements View.OnClickListener {

    TabLayout tl_tuijian;

    private RecyclerView rv_booksuggest;
    private BookDetailsAdapter adapter;


    private TextView tv_ballot;
    private LinearLayout ll_head_bg;
    private LinearLayout ll_details_one;
    private LinearLayout ll_details_two;
    private LinearLayout ll_fag;

    private ImageView img_suggest_book;

    private TextView tv_suggest_name;
    private TextView tv_fag;
    private TextView tv_fag_one;
    private TextView tv_fag_two;
    private TextView tv_fag_three;
    private TextView tv_fag_four;
    private TextView tv_details;
    private TextView tv_collet;
    private TextView tv_read;
    private TextView tv_directory;
    private TextView tv_more_one;
    private TextView tv_more_two;
    private TextView tv_num;
    private TextView tv_down;
    private ConstraintLayout cl_category;
    private String bookId;
    private boolean mCollect;
    private XRefreshView refreshView;
    DetailsBookResponse.DataBean mResponse;
    public static long lastRefreshTime;
    private int page = 0;
    //是否展开详情
    private boolean isExpand = false;

    @Override
    public int getContentViewId() {
        return R.layout.activity_bookdetails;
    }

    @Override
    public void findView() {
        setNoTitle();
        tl_tuijian = findViewById(R.id.tl_tuijian);
        rv_booksuggest = findViewById(R.id.rv_booksuggest);
        tv_ballot = findViewById(R.id.tv_ballot);
        ll_head_bg = findViewById(R.id.ll_head_bg);
        img_suggest_book = findViewById(R.id.img_suggest_book);
        tv_suggest_name = findViewById(R.id.tv_suggest_name);
        tv_fag = findViewById(R.id.tv_fag);
        tv_fag_one = findViewById(R.id.tv_fag_one);
        tv_fag_two = findViewById(R.id.tv_fag_two);
        tv_fag_three = findViewById(R.id.tv_fag_three);
        tv_fag_four = findViewById(R.id.tv_fag_four);
        tv_details = findViewById(R.id.tv_details);
        tv_directory = findViewById(R.id.tv_directory);
        tv_collet = findViewById(R.id.tv_collet);
        tv_read = findViewById(R.id.tv_read);
        tv_more_two = findViewById(R.id.tv_more_two);
        tv_more_one = findViewById(R.id.tv_more_one);
        ll_details_one = findViewById(R.id.ll_details_one);
        ll_details_two = findViewById(R.id.ll_details_two);
        tv_num = findViewById(R.id.tv_num);
        ll_fag = findViewById(R.id.ll_fag);
        refreshView = findViewById(R.id.bookdetail_view);
        cl_category = findViewById(R.id.cl_category);
        tv_down = findViewById(R.id.tv_down);
    }

    @Override
    public void setListener() {
        bookId = getIntent().getStringExtra("bookId");
        tv_ballot.setOnClickListener(this);
        tv_collet.setOnClickListener(this);
        tv_read.setOnClickListener(this);
        tv_more_two.setOnClickListener(this);
        tv_more_one.setOnClickListener(this);
        ll_fag.setOnClickListener(this);
        tv_details.setOnClickListener(this);
        cl_category.setOnClickListener(this);
        tv_down.setOnClickListener(this);
        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                page += 1;
                listAlsoViewBook(page * 20);
            }

        });
    }

    @Override
    public void afterCreate() {
        refreshView.setPinnedTime(1000);
        refreshView.setMoveForHorizontal(true);
        refreshView.setPullRefreshEnable(false);
        refreshView.setPullLoadEnable(true);
        refreshView.setAutoLoadMore(true);
        refreshView.enableReleaseToLoadMore(true);
        refreshView.enableRecyclerViewPullUp(true);
        refreshView.enablePullUpWhenLoadCompleted(true);
        // 设置上次刷新的时间
        refreshView.restoreLastRefreshTime(lastRefreshTime);

        rv_booksuggest.setNestedScrollingEnabled(false);
        rv_booksuggest.setFocusable(false);
        rv_booksuggest.setLayoutManager(new GridLayoutManager(this, 4));
        rv_booksuggest.getItemAnimator().setChangeDuration(0);// 通过设置动画执行时间为0来解决闪烁问题
        adapter = new BookDetailsAdapter(this);
        rv_booksuggest.setAdapter(adapter);
        afterbookDetail();
        getVipDistance();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_ballot) {
            if (PreUtils.getBoolean("tourist", false)) {
                Intent intent = new Intent();
                intent.setClass(BookDetailsActivity.this, LoginActivity.class);
                startActivity(intent);
            } else {
                new BollotBombPop(v, BookDetailsActivity.this, bookId, new BollotBombPop.PopReturnListener() {
                    @Override
                    public void onValueClick(String value) {
                        PopViewUtil.sendGifePop(v, BookDetailsActivity.this, value);
                    }
                });
            }

        } else if (v.getId() == R.id.tv_collet) {
            if (mCollect) {
                showToast("已收藏");
                return;
            }
            GroupPop.collentGroupPop(v, BookDetailsActivity.this, new GroupPop.PopSelectListener() {
                @Override
                public void onGroupIdClick(String group) {
                    if (group == null) {
                        showToast("请选择");
                        return;
                    }
                    post(Url.addBookcase, null, String.class, new FFNetWorkCallBack<String>() {
                        @Override
                        public void onSuccess(String response) {
                            if (response.contains("200")) {
                                showToast("收藏成功");
                                mCollect = true;
                                tv_collet.setText("已收藏");
                            }

                        }

                        @Override
                        public boolean onFail(String response) {
                            return false;
                        }
                    }, "{\"bookId\":\"" + bookId + "\",\"bookcaseGroupId\":\"" + group + "\"}");
                }
            });


        } else if (v.getId() == R.id.tv_read) {
            CollBookBean collBookBean = new CollBookBean();
            collBookBean.set_id(mResponse.getBookId());
            collBookBean.setAuthor(mResponse.getAuthor());
            collBookBean.setTitle(mResponse.getName());
            collBookBean.setShortIntro(StUtils.reBlank(mResponse.getBookIntro()));
            collBookBean.setCover(mResponse.getCoverImgUrl());
            collBookBean.setIsUpdate(true);
            collBookBean.setMonthSubscribe(mResponse.getIsMonthSubscribe());
            addToBookShelfUtils(collBookBean);

        } else if (v.getId() == R.id.tv_more_one) {
            Intent intent = new Intent(BookDetailsActivity.this, MoreBookActivity.class);
            intent.putExtra("MORE_Url", Url.listSimilarBook);
            intent.putExtra("MORE_Name", "同类作品推荐");
            intent.putExtra("MORE_Key", "book");
            intent.putExtra("MORE_Value", bookId);
            startActivity(intent);
        } else if (v.getId() == R.id.tv_more_two) {
            Intent intent = new Intent(BookDetailsActivity.this, MoreBookActivity.class);
            intent.putExtra("MORE_Url", Url.listAlsoViewBook);
            intent.putExtra("MORE_Name", "热门飙升");
            intent.putExtra("MORE_Key", "book");
            intent.putExtra("MORE_Value", bookId);
            startActivity(intent);
        } else if (v.getId() == R.id.ll_fag) {
            Intent intent = new Intent(BookDetailsActivity.this, AuthorActivity.class);
            intent.putExtra("author_name", mResponse.getAuthor());
            intent.putExtra("author_id", mResponse.getAuthorId());
            startActivity(intent);
        } else if (v.getId() == R.id.tv_details) {
            if (isExpand) {
                isExpand = false;
                tv_details.setMaxLines(3);// 收起
            } else {
                isExpand = true;
                tv_details.setMaxLines(Integer.MAX_VALUE);// 展开
            }
            if (TextUtils.isEmpty(mResponse.getBookIntro())) return;

            FFUtils.toggleEllipsize(BookDetailsActivity.this,
                    tv_details, 3,
                    StUtils.reBlank(mResponse.getBookIntro()),
                    StUtils.reBlank(mResponse.getBookIntro()),
                    R.color.nb_read_font_1, isExpand);
        } else if (v.getId() == R.id.cl_category) {
            new DetailsCategoryPop().categoryPopWindow(v, BookDetailsActivity.this, bookId, mResponse.getIsMonthSubscribe(), new DetailsCategoryPop.DetailsCategoryItemListener() {
                @Override
                public void onItemClick(int chapter, int page) {
                    CollBookBean collBookBean = new CollBookBean();
                    collBookBean.set_id(mResponse.getBookId());
                    collBookBean.setAuthor(mResponse.getAuthor());
                    collBookBean.setTitle(mResponse.getName());
                    collBookBean.setShortIntro(StUtils.reBlank(mResponse.getBookIntro()));
                    collBookBean.setCover(mResponse.getCoverImgUrl());
                    collBookBean.setIsUpdate(true);
                    collBookBean.setChaptersCount(chapter);
                    collBookBean.setMonthSubscribe(mResponse.getIsMonthSubscribe());
                    addToBookShelfUtils(collBookBean);
                }
            });
        } else if (v.getId() == R.id.tv_down) {
            if (PreUtils.getBoolean("tourist", false)) {
                Intent intent = new Intent();
                intent.setClass(BookDetailsActivity.this, LoginActivity.class);
                startActivity(intent);
                return;
            }

            new FFNetWork().post(Url.listChapter, null, new FFNetWorkCallBack<JListChapterBean>() {
                @Override
                public void onSuccess(JListChapterBean response) {
                    if (response.getCode() == 200) {
                        //获取目录数据
                        List<JListChapterBean.DataBean> data = response.getData();
                        List<TxtChapter> list = new ArrayList<>();
                        for (int i = 0; i < data.size(); i++) {
                            TxtChapter txtChapter = new TxtChapter();
                            txtChapter.setBookId(mResponse.getBookId());
                            txtChapter.setTitle(data.get(i).getChapterName());
                            txtChapter.setChapterId(data.get(i).getChapterId());
                            txtChapter.setVip(data.get(i).isIsVip());
                            txtChapter.setHasAuth(data.get(i).isHasAuth());
                            txtChapter.setMonthSubscribe(mResponse.getIsMonthSubscribe());
                            txtChapter.setPrice(data.get(i).getUserPrice() + "");
                            txtChapter.setJuan(data.get(i).getJuan());
                            list.add(txtChapter);
                        }
                        //拉起购买框
                        new DetailsDownMorePop().downMorePopWindow(v, BookDetailsActivity.this, list, 0, new DetailsDownMorePop.DownMoreListener() {
                            @Override
                            public void onItemClick(int pos) {

                            }
                        });
                    }

                }

                @Override
                public boolean onFail(JListChapterBean response) {
                    return false;
                }
            }, JListChapterBean.class, "{\"bookId\":\"" + bookId + "\"}");

        }

    }

    public void afterbookDetail() {
        post(Url.bookDetail, "", DetailsBookResponse.class, new FFNetWorkCallBack<DetailsBookResponse>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onSuccess(DetailsBookResponse response) {
                if (response.getCode() == 200) {
                    mResponse = response.getData();
                    tv_suggest_name.setText(mResponse.getName());
                    tv_fag.setText(mResponse.getAuthor());
                    FFUtils.toggleEllipsize(BookDetailsActivity.this,
                            tv_details, 3,
                            StUtils.reBlank(mResponse.getBookIntro()),
                            StUtils.reBlank(mResponse.getBookIntro()),
                            R.color.nb_read_font_1, isExpand);
                    tv_num.setText(mResponse.getKingTicket() + "");
                    tv_directory.setText("共" + mResponse.getChapterCount() + "章节 "
                            + mResponse.getWordCount() / 10000 + "万字  >");
                    if (mResponse.isInBookcase()) {
                        mCollect = true;
                        tv_collet.setText("已收藏");
                    } else {
                        mCollect = false;
                        tv_collet.setText("加收藏");
                    }

                     String category=mResponse.getCategory();
                   String[] list= category.split("-");
                    //不是青少年模式
                    if (!PreUtils.getBoolean("QING_switch", false)) {
                        if (list.length > 3) {
                            tv_fag_four.setVisibility(View.VISIBLE);
                            tv_fag_three.setVisibility(View.VISIBLE);
                            tv_fag_two.setVisibility(View.VISIBLE);
                            tv_fag_one.setVisibility(View.VISIBLE);
                            tv_fag_one.setText(list[0]);
                            tv_fag_two.setText(list[1]);
                            tv_fag_three.setText(list[2]);
                            tv_fag_four.setText(list[3]);
                        } else if (list.length == 3) {
                            tv_fag_one.setText(list[0]);
                            tv_fag_two.setText(list[1]);
                            tv_fag_three.setText(list[2]);
                            tv_fag_three.setVisibility(View.VISIBLE);
                            tv_fag_two.setVisibility(View.VISIBLE);
                            tv_fag_one.setVisibility(View.VISIBLE);
                            tv_fag_four.setVisibility(View.GONE);
                        } else if (list.length == 2) {
                            tv_fag_one.setText(list[0]);
                            tv_fag_two.setText(list[1]);
                            tv_fag_four.setVisibility(View.GONE);
                            tv_fag_three.setVisibility(View.GONE);
                            tv_fag_two.setVisibility(View.VISIBLE);
                            tv_fag_one.setVisibility(View.VISIBLE);
                        } else if (list.length == 1) {
                            tv_fag_one.setText(list[0]);
                            tv_fag_one.setVisibility(View.VISIBLE);
                            tv_fag_four.setVisibility(View.GONE);
                            tv_fag_three.setVisibility(View.GONE);
                            tv_fag_two.setVisibility(View.GONE);
                        }
                    } else {
                        tv_fag_one.setText("童话的书");
                        tv_fag_four.setVisibility(View.GONE);
                        tv_fag_three.setVisibility(View.GONE);
                        tv_fag_two.setVisibility(View.GONE);
                        tv_fag_one.setVisibility(View.GONE);
                    }
                    FFImageLoader.loadImage(BookDetailsActivity.this, img_suggest_book, mResponse.getCoverImgUrl(), mResponse.getBookId(), true);
                    MultiTransformation multi = new MultiTransformation(
                            new BlurTransformation(15),
                            new CropTransformation(200, 100, CropTransformation.CropType.TOP));
                    //Glide实现图片高斯模糊
                    Glide.with(BookDetailsActivity.this).load(mResponse.getCoverImgUrl())
                            .apply(bitmapTransform(multi)).into(new SimpleTarget<Drawable>() {
                                @Override
                                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                                    ll_head_bg.setBackground(resource);// 设置背景
                                }
                            });
                    setTagList(ll_details_one, getLayoutInflater(), mResponse.getTags());
                    setBookRoleList(ll_details_two, getLayoutInflater(), mResponse.getBookRole());
                }

            }

            @Override
            public boolean onFail(DetailsBookResponse response) {
                return false;
            }
        }, "{\"bookId\":\"" + bookId + "\"}");

        post(Url.listSimilarBook, null, BookListSimilarResponse.class, new FFNetWorkCallBack<BookListSimilarResponse>() {
            @Override
            public void onSuccess(BookListSimilarResponse response) {
                if (response.getCode() == 200) {
                    setTabs(tl_tuijian, getLayoutInflater(), response.getData());
                }
            }

            @Override
            public boolean onFail(BookListSimilarResponse response) {
                return false;
            }
        }, "{\"offset\":0,\"limit\":10,\"bookId\":\"" + bookId + "\"}");
        //加载看过此书的数据加载
        listAlsoViewBook(0);
    }

    public void listAlsoViewBook(int page) {
        post(Url.listAlsoViewBook, null, BookAlsoListResponse.class, new FFNetWorkCallBack<BookAlsoListResponse>() {
            @Override
            public void onSuccess(BookAlsoListResponse response) {
                if (response.getCode() == 200) {
                    if (response.getData() == null || response.getData().size() < 20) {
                        refreshView.setLoadComplete(true);
                    }
                    if (page == 0) {
                        refreshView.stopRefresh();
                        refreshView.setLoadComplete(false);
                        adapter.setData(response.getData());
                    } else {
                        refreshView.stopLoadMore();
                        adapter.addData(response.getData());
                    }
                }
            }

            @Override
            public boolean onFail(BookAlsoListResponse response) {
                return false;
            }
        }, "{\"offset\":" + page + ",\"limit\":20,\"bookId\":\"" + bookId + "\"}");

    }

    public void setTagList(LinearLayout linearLayout, LayoutInflater inflater, List<DetailsBookResponse.DataBean.TagsBean> list) {
        if (list != null && list.size() == 1) {
            if (StUtils.isEmpty(list.get(0).getName())) {
                return;
            }
        }
        List<TextView> listText = new ArrayList<>();
        linearLayout.removeAllViews();
        for (int i = 0; i < list.size(); i++) {
            View view = inflater.inflate(R.layout.booktag_item, null);
            view.setId(i);
            TextView textView = view.findViewById(R.id.tv_tag);
            textView.setText(list.get(i).getName());
            listText.add(textView);
            linearLayout.addView(view);
            int finalI = i;
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int s = 0; s < listText.size(); s++) {
                        if (s == finalI) {
                            listText.get(s).setBackgroundResource(R.drawable.item_tab_text_true);
                            listText.get(s).setTextColor(Color.parseColor("#ffffff"));
                        } else {
                            listText.get(s).setBackgroundResource(R.drawable.item_tab_text_false);
                            listText.get(s).setTextColor(Color.parseColor("#D3D3D3"));
                        }
                    }
                    Intent intent = new Intent();
                    intent.setClass(BookDetailsActivity.this, TabStackActivity.class);
                    intent.putExtra("TAB_ID", list.get(finalI).getTagId());
                    intent.putExtra("TAB_NAME", list.get(finalI).getName());
                    startActivity(intent);
                }
            });
        }
    }

    public void setBookRoleList(LinearLayout linearLayout, LayoutInflater inflater, DetailsBookResponse.DataBean.BookRoleBean bean) {
        List<Map<String, String>> list = new ArrayList<>();
        String[] protagonist = bean.getProtagonist().split("：");
        String[] costar = bean.getCostar().split("：");
        String[] other = bean.getOther().split("：");
        if (protagonist.length == 2) {
            String[] proTwo = protagonist[1].split("，");
            for (String pro : proTwo) {
                Map<String, String> map = new HashMap<>();
                map.put("key", protagonist[0]);
                map.put("value", pro);
                list.add(map);
            }
        } else {
            Map<String, String> map = new HashMap<>();
            map.put("key", protagonist[0]);
            map.put("value", "暂无");
            list.add(map);
        }
        if (costar.length == 2) {
            String[] costarTwo = costar[1].split("，");
            for (String pro : costarTwo) {
                Map<String, String> map = new HashMap<>();
                map.put("key", costar[0]);
                map.put("value", pro);
                list.add(map);
            }
        } else {
            Map<String, String> map = new HashMap<>();
            map.put("key", costar[0]);
            map.put("value", "暂无");
            list.add(map);
        }
        if (other.length == 2) {
            String[] otherTwo = other[1].split("，");
            for (String pro : otherTwo) {
                Map<String, String> map = new HashMap<>();
                map.put("key", other[0]);
                map.put("value", pro);
                list.add(map);
            }
        } else {
            Map<String, String> map = new HashMap<>();
            map.put("key", other[0]);
            map.put("value", "暂无");
            list.add(map);
        }


        linearLayout.removeAllViews();
        for (int i = 0; i < list.size(); i++) {
            View view = inflater.inflate(R.layout.book_role_item, null);
            TextView tv_name = view.findViewById(R.id.tv_name);
            TextView tv_role = view.findViewById(R.id.tv_role);
            tv_name.setText(list.get(i).get("value"));
            tv_role.setText(list.get(i).get("key"));
            linearLayout.addView(view);
        }
    }

    /**
     * 设置页卡显示效果
     *
     * @param tabLayout
     * @param inflater
     */
    private void setTabs(TabLayout tabLayout, LayoutInflater inflater, List<BookListSimilarResponse.DataBean> list) {
        tabLayout.removeAllTabs();
        for (int i = 0; i < list.size(); i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            View view = inflater.inflate(R.layout.allsuggest_item_book, null);
            // 使用自定义视图，目的是为了便于修改，也可使用自带的视图
            tab.setCustomView(view);
            ImageView img_grid_book = view.findViewById(R.id.img_grid_book);
            TextView txt_grid_book = view.findViewById(R.id.txt_grid_book);
            FFImageLoader.loadImage(BookDetailsActivity.this, img_grid_book, list.get(i).getCoverImgUrl(), list.get(i).getBookId(), true);
            txt_grid_book.setText(list.get(i).getName());
            int finalI = i;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(BookDetailsActivity.this, BookDetailsActivity.class);
                    intent.putExtra("bookId", list.get(finalI).getBookId());
                    startActivity(intent);
                }
            });
            tabLayout.addTab(tab);
        }
    }


    public class BookDetailsAdapter extends BaseRecyclerAdapter<DetailsViewHolder> {
        private Context context;


        private List<BookAlsoListResponse.DataBean> mData = new ArrayList<>();


        public BookDetailsAdapter(Context cnt) {
            this.context = cnt;

        }

        public void addData(List<BookAlsoListResponse.DataBean> data) {
            if (data != null) {
                mData.addAll(data);
                notifyDataSetChanged();
            }
        }

        public void setData(List<BookAlsoListResponse.DataBean> data) {
            this.mData.clear();
            addData(data);
        }

        /**
         * 创建ViewHolder
         */
        @Override
        public DetailsViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {

            View mView = LayoutInflater.from(context).inflate(R.layout.info_book_grid_item, parent, false);
            DetailsViewHolder viewHolder_text = new DetailsViewHolder(mView);
            return viewHolder_text;

        }

        @Override
        public DetailsViewHolder getViewHolder(View view) {
            return new DetailsViewHolder(view);
        }


        /**
         * 绑定数据：可以直接拿到已经绑定控件的Viewholder对象
         */
        @Override
        public void onBindViewHolder(final DetailsViewHolder holder, @SuppressLint("RecyclerView") int position, boolean isItem) {
            holder.txt_grid_book.setText(mData.get(position).getName());
            FFImageLoader.loadImage(context, holder.img_grid_book, mData.get(position).getCoverImgUrl(), mData.get(position).getBookId(), true);
            holder.tv_jindu.setVisibility(View.GONE);
            holder.ll_grid_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(BookDetailsActivity.this, BookDetailsActivity.class);
                    intent.putExtra("bookId", mData.get(position).getBookId());
                    startActivity(intent);

                }
            });


        }

        @Override
        public int getAdapterItemViewType(int position) {
            return position;
        }


        @Override
        public int getAdapterItemCount() {
            return mData.size();
        }


    }
    public void getVipDistance() {
        new FFNetWork().post(Url.vipDistance, null, new FFNetWorkCallBack<VipDistanceResponse>() {
            @Override
            public void onSuccess(VipDistanceResponse response) {
                if (response.getCode() == 200) {
                    ReadActivity.userIsVip = response.getData() <= 0;
                }
            }

            @Override
            public boolean onFail(VipDistanceResponse response) {
                return false;
            }
        }, VipDistanceResponse.class);
    }
    public void addToBookShelfUtils(CollBookBean collBookBean) {

        Intent intent = new Intent(BookDetailsActivity.this, ReadActivity.class);
        intent.putExtra(ReadActivity.EXTRA_COLL_BOOK, collBookBean);
        intent.putExtra(ReadActivity.EXTRA_IS_COLLECTED, true);
        startActivity(intent);

    }
}
